package io.renren.modules.lanling.controller;

import java.util.*;

import cn.hutool.core.lang.Validator;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import io.renren.common.validator.ValidatorUtils;
import io.renren.modules.lanling.service.CertificateService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.lanling.entity.AllSubjectEntity;
import io.renren.modules.lanling.service.AllSubjectService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;

import static io.renren.modules.sys.shiro.ShiroUtils.getUserId;


/**
 * 蓝领总科目表
 *
 * @author LANLING
 * @email lanling@gmail.com
 * @date 2018-11-26 09:58:39
 */
@RestController
@RequestMapping("lanling/allsubject")
public class AllSubjectController {
    @Autowired
    private AllSubjectService allSubjectService;
    @Autowired
    private CertificateService certificateService;


    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("lanling:allsubject:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = allSubjectService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("lanling:allsubject:info")
    public R info(@PathVariable("id") Integer id) {
        AllSubjectEntity allSubject = allSubjectService.selectById(id);

        return R.ok().put("allSubject", allSubject);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("lanling:allsubject:save")
    public R save(@RequestBody AllSubjectEntity allSubject) {
        //todo 判断重复
        EntityWrapper<AllSubjectEntity> entityEntityWrapper = new EntityWrapper<>();
        entityEntityWrapper.eq("subject_name", allSubject.getSubjectName());
        int i = allSubjectService.selectCount(entityEntityWrapper);
        if (i > 0) {
            return R.error("请勿添加重复数据");
        }

        allSubject.setCreateDate(new Date());
        allSubjectService.insert(allSubject);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("lanling:allsubject:update")
    @Transactional(rollbackFor = Exception.class)
    public R update(@RequestBody AllSubjectEntity allSubject) {
        ValidatorUtils.validateEntity(allSubject);
        allSubject.setLastModifyDate(new Date());
        //全部更新
        allSubjectService.updateAllColumnById(allSubject);
        //同步证书名称
        Map<String, Object> params = new HashMap<>();
        params.put("certificateName", allSubject.getCertificateName());
        params.put("allSubjectId", allSubject.getId());
        
        certificateService.updateCertificate(params);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("lanling:allsubject:delete")
    @Transactional(rollbackFor = Exception.class)
    public R delete(@RequestBody Integer[] ids) {
        List<Integer> idList = Arrays.asList(ids);

        //同步删除证书列表
        certificateService.deleteByAllSubjectId(idList);

        allSubjectService.deleteBatchIds(idList);

        return R.ok();
    }


    @RequestMapping("/getAllSubjectList")
    public R getAllSubjectList(Integer typeId) {
        EntityWrapper<AllSubjectEntity> entityEntityWrapper = new EntityWrapper<>();
        entityEntityWrapper.orderBy("subject_name", false);

        Long userId = getUserId();
        if (userId != 1) {
            entityEntityWrapper.ne("type_id", 0);
        }

        if (typeId != null) {
            entityEntityWrapper.eq("type_id", typeId);
        }

        return R.ok().put("list", allSubjectService.selectList(entityEntityWrapper));
    }
}
